// 封装请求函数
function request(options) {
    return new Promise((resolve, reject) => {
        uni.request({
            url: options.url,
            method: options.method || 'GET',
            data: options.data || {},
            header: options.header || {
                'content-type': 'application/json'
            },
            success: (res) => {
                if (res.statusCode === 200) {
                    resolve(res.data);
                } else {
                    reject(new Error(`请求失败，状态码：${res.statusCode}`));
                }
            },
            fail: (err) => {
                reject(err);
            }
        });
    });
}

// 封装 GET 请求
function get(url, data = {}, header = {}) {
    return request({
        url,
        method: 'GET',
        data,
        header
    });
}

// 封装 POST 请求
function post(url, data = {}, header = {}) {
    return request({
        url,
        method: 'POST',
        data,
        header
    });
}

// 封装 PUT 请求
function put(url, data = {}, header = {}) {
    return request({
        url,
        method: 'PUT',
        data,
        header
    });
}

// 封装 DELETE 请求
function del(url, data = {}, header = {}) {
    return request({
        url,
        method: 'DELETE',
        data,
        header
    });
}

export default {
    get,
    post,
    put,
    del
};    